package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Cargo;
import com.blb.entity.CargoArea;
import com.blb.entity.Shelves;
import com.blb.entity.User;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.ICargoAreaService;
import com.blb.service.IShelvesService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Controller
@RequestMapping("/shelves")
public class ShelvesController {
    @Resource
    private IShelvesService shelvesService;
    @Resource
    private ICargoAreaService cargoAreaService;

    @RequestMapping("{page}")
    public String shelves(@PathVariable String page){
        return "warehouse/"+page;
    }

    @RequiresPermissions("shelves_manage")
    @RequestMapping("allShelves")
    public String getAll(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        model.addAttribute("companyId",companyId);
        int i = shelvesService.count(new QueryWrapper<Shelves>().eq("company_id",companyId));
        model.addAttribute("i",i);
        return "warehouse/shelf_manage";
    }

    /**
     * 分页-模糊查询
     */
    @ResponseBody
    @RequestMapping("findByLike")
    public BlbPage findByLike(@RequestParam(required = false,value = "nodeId")  String nodeId,@RequestParam(required = false,value = "nodeType")  String nodeType,
                              @RequestParam(required = false,value = "companyId") String companyId,
                              @RequestParam(required = false,value = "shelvesNo") String shelvesNo,
                              @RequestParam(required = false,value = "creatTime")String creatTime,int start, int length){
        System.out.println(nodeId);
        System.out.println(nodeType);
        System.out.println(companyId);
        //分页数据
        List<Shelves> allShelves = shelvesService.getAllShelves(nodeId,nodeType,companyId,shelvesNo, creatTime, start, length);
        //总记录数
        int count = shelvesService.count(nodeId,nodeType,companyId,shelvesNo, creatTime);

        return new DataTablePage(count,count,allShelves);
    }
    /**
     *  @ResponseBody
     *     @RequestMapping("findMaxNum")
     *     public List<Integer> findMaxNum(String shelvesId){
     *         Shelves shelves = shelvesService.findShelvesById(shelvesId);
     *         Integer dn = shelves.getTotalNum();
     *         int count = cargoService.count(new QueryWrapper<Cargo>().eq("shelves_id",shelvesId));
     *         ArrayList<Integer> list = new ArrayList<>();
     *         list.add(dn);
     *         list.add(count);
     *         System.out.println(list);
     *         return list;
     *     }
     */
    @ResponseBody
    @RequestMapping("findMaxNum")
    public List<Integer> findMaxNum(String damId){
        CargoArea cargoArea = cargoAreaService.getById(damId);
        Integer num = cargoArea.getShelvesNum();
        int count = shelvesService.count(new QueryWrapper<Shelves>().eq("dam_id", damId));
        ArrayList<Integer> list = new ArrayList<>();
        list.add(num);
        list.add(count);
        return list;
    }

    /*add货架*/
    @PostMapping("addShelves")
    public String addShelves(@RequestBody Shelves shelves){
        User user = ShiroUtils.getUser();
        shelves.setCompanyId(user.getCompanyId());
        String i = String.valueOf(shelvesService.count() + 1);
        shelves.setId("s"+i);
        shelves.setCreatTime(new Date());
        shelves.setStatus("0");
        System.out.println(shelves);
        shelvesService.save(shelves);
        return "warehouse/shelf_manage";
    }
    /**
     * updateShelves
     * @param shelves
     * @return
     */
   @PostMapping("updateShelves")
   public String updateShelves(@RequestBody Shelves shelves){
       System.out.println("cnm"+shelves);

       shelvesService.updateShelves(shelves);

       return "warehouse/shelf_manage";
   }
   /*更新货架状态*/
   @RequestMapping("updateShelvesStatus")
   @ResponseBody
   public String updateShelvesStatus(String id,String status){
       System.out.println("ID:"+id+"status"+status);
       shelvesService.updateShelvesStatus(id,status);
       return "aaa";
   }
    /**
     * findShelvesById
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("findShelvesById")
    public Shelves findShelvesById(String id){
        Shelves shelves = shelvesService.findShelvesById(id);
        System.out.println(shelves);
        return shelves;
    }
}

